/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.dao.impl;

import com.wisnu.scs.dao.DbBarangKeluarDao;
import com.wisnu.scs.entity.DbBarang;
import com.wisnu.scs.entity.DbBarangKeluar;
import com.wisnu.scs.error.ErrorMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class DbBarangKeluarDaoImpl implements DbBarangKeluarDao {

    private final SessionFactory sessionFactory;

    public DbBarangKeluarDaoImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void insertBarangKeluar(DbBarangKeluar barangKeluar, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.save(barangKeluar);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void updateBarangKeluar(DbBarangKeluar barangKeluar, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.update(barangKeluar);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void deleteKeluar(DbBarangKeluar barangKeluar, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.delete(barangKeluar);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public List<DbBarangKeluar> selectBarangKeluarByDate(String searchText, Date date) throws ErrorMessage {
        List<DbBarangKeluar> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangKeluar where TANGGAL_KELUAR = :date")
                    .setDate("date", date).list();

            ListIterator<DbBarangKeluar> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(searchText)) {
                    iterator.remove();
                }
            }

            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangKeluar> selectBarangKeluarByDateRange(String searchText, List<Date> dateList) throws ErrorMessage {
        List<DbBarangKeluar> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangKeluar where TANGGAL_KELUAR >= :firstDate AND TANGGAL_KELUAR <= :lastDate")
                    .setDate("firstDate", dateList.get(0))
                    .setDate("lastDate", dateList.get(1))
                    .list();

            ListIterator<DbBarangKeluar> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(searchText)) {
                    iterator.remove();
                }
            }

            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangKeluar> selectAllBarangKeluar() throws ErrorMessage {
        List<DbBarangKeluar> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangKeluar").list();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangKeluar> selectBarangKeluarByKeyWords(String searchText) throws ErrorMessage {
        List<DbBarangKeluar> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangKeluar").list();
            
            ListIterator<DbBarangKeluar> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(searchText)) {
                    iterator.remove();
                }
            }
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

}
